package com.chuangshixinchen.enterprise.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangshixinchen.enterprise.common.JwtUtils;
import com.chuangshixinchen.enterprise.common.Result;
import com.chuangshixinchen.enterprise.common.StatusCode;
import com.chuangshixinchen.enterprise.entity.Tb_user;
import com.chuangshixinchen.enterprise.service.ITb_userService;
import com.chuangshixinchen.enterprise.vo.req.UserChangePwdVo;
import com.chuangshixinchen.enterprise.vo.req.UserLoginVo;
import com.chuangshixinchen.enterprise.vo.req.UserReqPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "用户模块", tags = "获取用户模块信息")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ITb_userService userService;

    @ApiOperation(value = "登录", notes = "用户名密码登录")
    @PostMapping(value = "/login")
    @ResponseBody
    public Result<Tb_user> login(@RequestBody UserLoginVo vo) {
        Result<Tb_user> result = userService.login(vo);
        return result;
    }

    @ApiOperation(value = "修改", notes = "修改密码")
    @PutMapping(value = "/changePwd")
    @ResponseBody
    public Result changePwd(@RequestHeader("Authorization") String token, @RequestBody UserChangePwdVo vo) {
        if (!JwtUtils.checkToken(token)){
            return new Result<Page<Tb_user>>(false, StatusCode.AUTHORIZATION_ERROR,"授权失败！");
        }
        Result result = userService.changePwd(token,vo);
        return result;
    }

    @ApiOperation(value = "查询", notes = "根据token查询用户详情")
    @GetMapping(value = "/userInfo/{id}")
    @ResponseBody
    public Result<Tb_user> userInfo(@RequestHeader("Authorization") String token,@PathVariable(value = "id") Long id) {
        if (!JwtUtils.checkToken(token)){
            return new Result<Tb_user>(false, StatusCode.AUTHORIZATION_ERROR,"授权失败！");
        }
        Result<Tb_user> result = userService.getInfo(id);
        return result;
    }

    @ApiOperation(value = "查询", notes = "分页查询用户")
    @PostMapping(value = "/page")
    @ResponseBody
    public Result<Page<Tb_user>> page(@RequestHeader("Authorization") String token, @RequestBody UserReqPageVo vo) {
        if (!JwtUtils.checkToken(token)){
            return new Result<Page<Tb_user>>(false, StatusCode.AUTHORIZATION_ERROR,"授权失败！");
        }
        Result<Page<Tb_user>> result = userService.listPage(vo);
        return result;
    }

    @ApiOperation(value = "新增", notes = "新增一个用户")
    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestHeader("Authorization") String token, @RequestBody Tb_user user) {
        if (!JwtUtils.checkToken(token)){
            return new Result<Page<Tb_user>>(false, StatusCode.AUTHORIZATION_ERROR,"授权失败！");
        }
        Result result = userService.saveInfo(user);
        return result;
    }

    @ApiOperation(value = "修改", notes = "修改一个用户")
    @PutMapping(value = "/update")
    @ResponseBody
    public Result update(@RequestHeader("Authorization") String token, @RequestBody Tb_user user) {
        if (!JwtUtils.checkToken(token)){
            return new Result<Page<Tb_user>>(false, StatusCode.AUTHORIZATION_ERROR,"授权失败！");
        }
        Result result = userService.updateInfo(user);
        return result;
    }
}
